package com.hjqjl.whdemo.feature.feature0.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;

import com.blankj.utilcode.util.LogUtils;
import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivityWorkManagerBinding;
import com.hjqjl.whdemo.workmanager.MyWorker;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class WorkManagerActivity extends BaseActivity {
    private int count = 0;

    public static void start(Context context) {
        Intent starter = new Intent(context, WorkManagerActivity.class);
        context.startActivity(starter);
    }

    private ActivityWorkManagerBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWorkManagerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnWorkManager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Constraints constraints = new Constraints.Builder()
                        .setRequiredNetworkType(NetworkType.CONNECTED)//网络链接的时候使用,避免各种网络判断,省时省力
//                        .setRequiresBatteryNotLow(true)//是否在低电量的时候执行
//                        .setRequiresStorageNotLow(true)//是否在内存不足的时候执行
//                        .setRequiresCharging(true)//是否时充电的时候执行
                        .build();
                WorkRequest uploadWorkRequest =
                        new OneTimeWorkRequest.Builder(MyWorker.class)
                                .setConstraints(constraints)//设置触发条件
                                .setInitialDelay(10, TimeUnit.SECONDS)//符合触发条件后，延迟10秒执行
                                .build();

                WorkManager workManager = WorkManager.getInstance(pActivity);
                workManager.getWorkInfoByIdLiveData(uploadWorkRequest.getId())
                        .observe(pActivity, workInfo -> {//更新UI
                            if (workInfo != null && workInfo.getState() == WorkInfo.State.SUCCEEDED) {
                                LogUtils.v("count" + count);
                                count++;
                                binding.tvShow.setText(count + "--workManager--" + new Random().nextInt(100));
                            }
                        });
                workManager.enqueue(uploadWorkRequest);
//                WorkManager
//                        .getInstance(pActivity)
//                        .enqueue(uploadWorkRequest);

                //存在同一个worker的情况下不会在添加重复任务
//                WorkManager
//                        .getInstance(pActivity)
//                        .enqueueUniqueWork("aa", ExistingWorkPolicy.KEEP, (OneTimeWorkRequest) uploadWorkRequest);

            }
        });

        binding.btnWorkManagerLoop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PeriodicWorkRequest periodicWorkRequest =
                        new PeriodicWorkRequest.Builder(MyWorker.class, 15, TimeUnit.MINUTES)
                                .build();
                WorkManager workManager = WorkManager.getInstance(pActivity);
                workManager.getWorkInfoByIdLiveData(periodicWorkRequest.getId())
                        .observe(pActivity, workInfo -> {
                            if (workInfo != null && workInfo.getState() == WorkInfo.State.RUNNING) {
                                LogUtils.v("count" + count);
                                count++;
                                binding.tvShow.setText(count + "workManager" + new Random().nextInt(100));
                            }
                        });
//                workManager.enqueue(periodicWorkRequest);
                workManager.enqueueUniquePeriodicWork("MyUniqueWorkName",
                        ExistingPeriodicWorkPolicy.KEEP,
                        periodicWorkRequest);
            }
        });
    }

}